package com.dianping.cat.consumer.all.config.transform;

import com.dianping.cat.consumer.all.config.IEntity;
import com.dianping.cat.consumer.all.config.IVisitor;
import com.dianping.cat.consumer.all.config.entity.AllConfig;
import com.dianping.cat.consumer.all.config.entity.Name;
import com.dianping.cat.consumer.all.config.entity.Report;
import com.dianping.cat.consumer.all.config.entity.Type;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;










public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  30 */   public DefaultXmlBuilder() { this(false); }



/*  34 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  38 */     this.m_compact = compact;
/*  39 */     this.m_sb = sb;
/*  40 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  44 */     entity.accept(this.m_visitor);
/*  45 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  49 */     this.m_level--;

/*  51 */     indent();
/*  52 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  56 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  60 */     if (value == null) {
/*  61 */       return null;
    }

/*  64 */     String str = toString(value);
/*  65 */     int len = str.length();
/*  66 */     StringBuilder sb = new StringBuilder(len + 16);

/*  68 */     for (int i = 0; i < len; i++) {
/*  69 */       char ch = str.charAt(i);

/*  71 */       switch (ch) {
        case '<':
/*  73 */           sb.append("&lt;");
          break;
        case '>':
/*  76 */           sb.append("&gt;");
          break;
        case '&':
/*  79 */           sb.append("&amp;");
          break;
        case '"':
/*  82 */           if (!text) {
/*  83 */             sb.append("&quot;");
            break;
          }
        default:
/*  87 */           sb.append(ch);
          break;
      }

    }
/*  92 */     return sb.toString();
  }

  protected void indent() {
/*  96 */     if (!this.m_compact) {
/*  97 */       for (int i = this.m_level - 1; i >= 0; i--) {
/*  98 */         this.m_sb.append("   ");
      }
    }
  }


/* 104 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 108 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 112 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 116 */     indent();

/* 118 */     this.m_sb.append('<').append(name);

/* 120 */     int len = nameValues.length;

/* 122 */     for (int i = 0; i + 1 < len; i += 2) {
/* 123 */       Object attrName = nameValues[i];
/* 124 */       Object attrValue = nameValues[i + 1];

/* 126 */       if (attrValue != null) {
/* 127 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 131 */     if (dynamicAttributes != null) {
/* 132 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 133 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 137 */     if (text != null && closed) {
/* 138 */       this.m_sb.append('>');
/* 139 */       this.m_sb.append(escape(text, true));
/* 140 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 142 */       if (closed) {
/* 143 */         this.m_sb.append('/');
      } else {
/* 145 */         this.m_level++;
      }

/* 148 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 154 */     if (value instanceof String)
/* 155 */       return (String)value; 
/* 156 */     if (value instanceof Collection) {
/* 157 */       Collection<Object> list = (Collection)value;
/* 158 */       StringBuilder sb = new StringBuilder(32);
/* 159 */       boolean first = true;

/* 161 */       for (Object item : list) {
/* 162 */         if (first) {
/* 163 */           first = false;
        } else {
/* 165 */           sb.append(',');
        }

/* 168 */         if (item != null) {
/* 169 */           sb.append(item);
        }
      }

/* 173 */       return sb.toString();
/* 174 */     }  if (value.getClass().isArray()) {
/* 175 */       int len = Array.getLength(value);
/* 176 */       StringBuilder sb = new StringBuilder(32);
/* 177 */       boolean first = true;

/* 179 */       for (int i = 0; i < len; i++) {
/* 180 */         Object item = Array.get(value, i);

/* 182 */         if (first) {
/* 183 */           first = false;
        } else {
/* 185 */           sb.append(',');
        }

/* 188 */         if (item != null) {
/* 189 */           sb.append(item);
        }
      }

/* 193 */       return sb.toString();
    }

/* 196 */     return String.valueOf(value);
  }


  public void visitAllConfig(AllConfig allConfig) {
/* 201 */     startTag("all-config", null, new Object[0]);

/* 203 */     if (!allConfig.getReports().isEmpty()) {
/* 204 */       for (Report report : allConfig.getReports().values()) {
/* 205 */         report.accept(this.m_visitor);
      }
    }

/* 209 */     endTag("all-config");
  }



/* 214 */   public void visitName(Name name) { startTag("name", true, null, new Object[] { "id", name.getId() }); }



  public void visitReport(Report report) {
/* 219 */     startTag("report", null, new Object[] { "id", report.getId() });

/* 221 */     if (!report.getTypes().isEmpty()) {
/* 222 */       for (Type type : report.getTypes().values()) {
/* 223 */         type.accept(this.m_visitor);
      }
    }

/* 227 */     endTag("report");
  }


  public void visitType(Type type) {
/* 232 */     startTag("type", null, new Object[] { "id", type.getId() });

/* 234 */     if (!type.getNameList().isEmpty()) {
/* 235 */       for (Name name : type.getNameList()) {
/* 236 */         name.accept(this.m_visitor);
      }
    }

/* 240 */     endTag("type");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/all/config/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */